/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.model;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * A partField is a part of a field, containing a certain type of crop
 * A partField only exists for the duration of the growing season.
 * Where fields are boundried by natural borders like roads and water,
 * partFields are boundried by the region their crop is sown.
 *
 * PartField shapes can contains holes
 *
 *
 * @author Timon Veenstra
 */
@NamedQueries(value = {
    @NamedQuery(name = PartField.Q_FIND_ALL_PARTFIELDS, query = "select p from PartField p"),
    @NamedQuery(name = PartField.Q_FIND_ALL_PARTFIELDS_BY_DATE, query = "select p from PartField p where :date between p.startDate and p.endDate")
})
@Entity
@Table(name = "PARTFIELD")
public class PartField extends Surface implements Serializable, Persistable{
    public static final String Q_FIND_ALL_PARTFIELDS = "findAllPartFields";
    public static final String Q_FIND_ALL_PARTFIELDS_BY_DATE = "findAllPartFieldsByDate";
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FIELD_FILE_ID")
    private FieldFile fieldFile;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FIELD_ID")
    private Field field;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "FIELD_PLAN_ID")
    private FieldPlan fieldPlan;

    @Column(name = "CROP_LIST_MODULE")
    private String cropListModule;

    @Column(name = "CROP_KEY")
    private String cropKey;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PRODUCTION_UNIT_ID")
    private ProductionUnit productionUnit;
    
    @OneToMany(cascade=CascadeType.ALL, mappedBy="partField")
    private Set<Task> tasks;
    
    public long getId() {
        return id;
    }
    
    public FieldFile getFieldFile() {
        return this.fieldFile;
    }

    public void setFieldFile(FieldFile fieldFile) {
        this.fieldFile = fieldFile;
    }

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

    public String getCropKey() {
        return cropKey;
    }

    public void setCropKey(String cropKey) {
        this.cropKey = cropKey;
    }

    public String getCropListModule() {
        return cropListModule;
    }

    public void setCropListModule(String cropListModule) {
        this.cropListModule = cropListModule;
    }

    public ProductionUnit getProductionUnit() {
        return productionUnit;
    }

    public void setProductionUnit(ProductionUnit productionUnit) {
        this.productionUnit = productionUnit;
    }

    public nl.cloudfarming.client.model.FieldPlan getFieldPlan() {
        return fieldPlan;
    }

    public void setFieldPlan(nl.cloudfarming.client.model.FieldPlan FieldPlan) {
        this.fieldPlan = FieldPlan;
    }

    public Set<Task> getTasks() {
        return tasks;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PartField other = (PartField) obj;
        if (this.id != other.id) {
            return false;
        }
        if ((this.getName() == null) ? (other.getName() != null) : !this.getName().equals(other.getName())) {
            return false;
        }
        if (this.fieldFile != other.fieldFile && (this.fieldFile == null || !this.fieldFile.equals(other.fieldFile))) {
            return false;
        }
        if (this.field != other.field && (this.field == null || !this.field.equals(other.field))) {
            return false;
        }
        if (this.getStartDate() != other.getStartDate() && (this.getStartDate() == null || !this.getStartDate().equals(other.getStartDate()))) {
            return false;
        }
        if (this.getEndDate() != other.getEndDate() && (this.getEndDate() == null || !this.getEndDate().equals(other.getEndDate()))) {
            return false;
        }
        if ((this.cropListModule == null) ? (other.cropListModule != null) : !this.cropListModule.equals(other.cropListModule)) {
            return false;
        }
        if ((this.cropKey == null) ? (other.cropKey != null) : !this.cropKey.equals(other.cropKey)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + (int) (this.id ^ (this.id >>> 32));
        hash = 97 * hash + (this.getName() != null ? this.getName().hashCode() : 0);
        hash = 97 * hash + (this.fieldFile != null ? this.fieldFile.hashCode() : 0);
        hash = 97 * hash + (this.field != null ? this.field.hashCode() : 0);
        hash = 97 * hash + (this.getStartDate() != null ? this.getStartDate().hashCode() : 0);
        hash = 97 * hash + (this.getEndDate() != null ? this.getEndDate().hashCode() : 0);
        hash = 97 * hash + (this.cropListModule != null ? this.cropListModule.hashCode() : 0);
        hash = 97 * hash + (this.cropKey != null ? this.cropKey.hashCode() : 0);
        return hash;
    }
    
      /**
     * PartField builder class
     *
     * usage: PartField partfield = new PartField.Builder().setName("someName").build();
     *
     */
    public static class Builder {
        private PartField partField;
        
        
        public Builder() {
            partField = new PartField();
        }
        
        public Builder setStartDate(Date startDate){
            partField.setStartDate(startDate);
            return this;
        }
        
        public Builder setEndDate(Date endDate){
            partField.setEndDate(endDate);
            return this;
        }
        
        public Builder setProductionUnit(ProductionUnit productionUnit){
            partField.setProductionUnit(productionUnit);
            return this;
        }
        
        public Builder setAcreage(Double acreage){
            partField.setAcreage(acreage);
            return this;
        }
        
        public PartField build() {
            return partField;
        }
    }

    
}
